<?php
echo $this->loadJs();
echo $this->loadCss();
$this->subtitulo = "Maquinas virtuales";
?>
<style type="text/css">
    .codigo_maquina_virtual
    {
        display: none;
    }
</style>
<script type="text/javascript">
    var oTable = null;    
    
    $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
    {
        if ( typeof sNewSource != 'undefined' && sNewSource != null ) {
            oSettings.sAjaxSource = sNewSource;
        }
    
        // Server-side processing should just call fnDraw
        if ( oSettings.oFeatures.bServerSide ) {
            this.fnDraw();
            return;
        }
    
        this.oApi._fnProcessingDisplay( oSettings, true );
        var that = this;
        var iStart = oSettings._iDisplayStart;
        var aData = [];
    
        this.oApi._fnServerParams( oSettings, aData );
    
        oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
            /* Clear the old information from the table */
            that.oApi._fnClearTable( oSettings );
        
            /* Got the data - add it to the table */
            var aData =  (oSettings.sAjaxDataProp !== "") ?
                that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
        
            for ( var i=0 ; i<aData.length ; i++ )
            {
                that.oApi._fnAddData( oSettings, aData[i] );
            }
        
            oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        
            if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
            {
                oSettings._iDisplayStart = iStart;
                that.fnDraw( false );
            }
            else
            {
                that.fnDraw();
            }
        
            that.oApi._fnProcessingDisplay( oSettings, false );
        
            /* Callback user function - for event handlers etc */
            if ( typeof fnCallback == 'function' && fnCallback != null )
            {
                fnCallback( oSettings );
            }
        }, oSettings );
    };       
    
    function reloadTable() {
        if (oTable) {
            oTable.fnDestroy();
        }
        oTable = $('#vm-table').dataTable( {                                                
            "oLanguage": {
                "sLengthMenu": "_MENU_ records per page"
            },
            "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
            "sPaginationType": "bootstrap",
            aoColumnsDef: [],
            bLengthChange: false,
            "aoColumns": [
                {
                    "sTitle": " ",
                    "sName": "codigo_maquina_virtual",
                    "sClass": "codigo_maquina_virtual"
                },
                {
                    "sTitle": " ",
                    "sName": "delete"
                },
                {
                    "sTitle": "Maquina Virtual",
                    "sName": "vm"
                },
//                {
//                    "sTitle": "Asignar Alumnos",
//                    "sName": "assign"
//                },
                {
                    "sTitle": "Template",
                    "sClass": "so"
                },                        
                {
                    "sTitle": "Hardware"
                }
                ,
                {
                    "sTitle": "IP",                            
                    "sClass": "networkAdd"
                },
                {
                    "sTitle": "Netmask",
                    "sName": "netmask"
                },
                {
                    "sTitle": "Estado",
                    "sName": "estado",
                    "sClass": 'estado'
                }
            ],
            "sAjaxSource": "/admin/virtualmachine/listbylab/laboratorio/<?php echo $this->laboratorio; ?>",
    
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                $.ajax({
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback,
                    "contentType": "application/json; charset=utf-8",
                    "dataType": "json",
                    "cache": false,
                    "error": function (xhr, error, thrown) {                        
                        $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + thrown + '</div>');
                    }
                });    
            }
    
            
        });                 
        checkStatus();
        setTimeout(controlTable, 1000);

    }
    function controlTable(){
        $('#vm-table').find('tr').each(function() {            
            var s = $(this).find('.estado').html();
            if (s === 'A') {
                $(this).addClass('vm-created success');
                $(this).addClass('success');
            } else if (s === 'E') {                    
                $(this).addClass('vm-error error');
                $(this).addClass('error');                               
            }
        });}
    function deletevm(vmId) {        
        bootbox.confirm("Desea eliminar la maquina virtual?", function(result) {
            if(result) {
                $.ajax({
                    url:'/admin/virtualmachine/deletevm/codigo_maquina_virtual/' + vmId,                
                    type: 'post',
                    dataType: 'json',                
                    success: function(data) {
                        if(data.response == "success") {
                            reloadTable();
                        } else {
                            alert('Mal :\'(' + data.message);
                            $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error!</strong>' + data.message + '</div>');
                        }
                    }
                });
            } 
        });
    };
    
    function checkStatus() {
        $.ajax({
            url: '/admin/virtualmachine/checkdeployjobstatus/codigo_laboratorio/' + <?php echo $this->laboratorio; ?>,
            type: 'post',
            dataType: 'json',                
            success: function(data) {                
                if(data.response == "refresh") {                    
                    setTimeout(reloadTable, 10000);
                } 
            }
        });
    }   
    
    $(document).ready(function() {
    
        menuClass('laboratorio');
        $.blockUI({
            message: "Aguarde un momento por favor"
        });      
        
        reloadTable();                
        $.unblockUI();
         

    });

</script>
<input type="hidden" id="laboratorio" name="laboratorio" value="<?php echo $this->laboratorio; ?>"/>
<h3>Laboratorio: <?php echo $this->nombreLab; ?></h3>
<div id="div-alert" class="row-fluid"></div>
<br/>
<div id="vm-container">
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="vm-table"></table>
</div>

